﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Reflection;
using Newtonsoft.Json;

namespace PMS
{
    public class ModelTools
    {
        /// <summary>
        /// 将表单转化为实体，仅限查询用，因此空值会被实体过滤掉
        /// </summary>
        /// <param name="Form"></param>
        /// <param name="obj"></param>
        public static void Form2Model(NameValueCollection Form, Object obj)
        {
            //得到type
            Type type = obj.GetType();
            //得到所有set属性
            PropertyInfo[] pis = type.GetProperties(BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance);
            //循环数组里面每一个PropertyInfo
            foreach (PropertyInfo pi in pis)
            {
                //得到属性名称
                string name = pi.Name;
                //更具属性名称去表单里面取值  表单的name与属性名称必须一致
                var value = Form[name];
                var ptype = pi.PropertyType;
                //判断这个是否为空
                if (value != null && !"".Equals(value))
                {
                    if (ptype.IsGenericType && ptype.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类
                    {
                        //如果type为nullable类，声明一个NullableConverter类，该类提供从Nullable类到基础基元类型的转换
                        System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(ptype);
                        //将type转换为nullable对的基础基元类型
                        ptype = nullableConverter.UnderlyingType;
                    }
                    //调用SetValue设置值
                    try
                    {
                        pi.SetValue(obj, Convert.ChangeType(value, ptype));
                    }
                    catch (Exception)
                    {


                    }

                }
            }
        }


        /// <summary>
        /// 将表单转化为实体，仅限查询用，因此空值会被实体过滤掉
        /// </summary>
        /// <param name="Form"></param>
        /// <param name="obj"></param>
        public static void Form2EditModel(NameValueCollection Form, Object obj)
        {
            //得到type
            Type type = obj.GetType();
            //得到所有set属性
            PropertyInfo[] pis = type.GetProperties(BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance);
            //循环数组里面每一个PropertyInfo
            foreach (PropertyInfo pi in pis)
            {
                //得到属性名称
                string name = pi.Name;
                //更具属性名称去表单里面取值  表单的name与属性名称必须一致
                var value = Form[name];
                var ptype = pi.PropertyType;
                //判断这个是否为空
                if (value != null)
                {
                    if (ptype.IsGenericType && ptype.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类
                    {
                        //如果type为nullable类，声明一个NullableConverter类，该类提供从Nullable类到基础基元类型的转换
                        System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(ptype);
                        //将type转换为nullable对的基础基元类型
                        ptype = nullableConverter.UnderlyingType;
                    }
                    //调用SetValue设置值
                    if (value == "" && (ptype == typeof(int) || ptype == typeof(float) || ptype == typeof(double) || ptype == typeof(decimal)))
                    {
                        value = "0";
                    }
                    pi.SetValue(obj, Convert.ChangeType(value, ptype));
                }
            }
        }


        public static void GetSearchContionsValue<T>(NameValueCollection Form, List<SearchCondition> listSearch)
        {
            //得到type
            Type type = typeof(T);
            //得到所有set属性
            PropertyInfo[] pis = type.GetProperties(BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance);
            //循环数组里面每一个PropertyInfo
            foreach (PropertyInfo pi in pis)
            {
                //得到属性名称
                string name = pi.Name;
                //更具属性名称去表单里面取值  表单的name与属性名称必须一致
                var value = Form[name];
                //判断这个是否为空
                if (value != null && !"".Equals(value))
                {
                    listSearch.Where(r => r.Field == name).FirstOrDefault().Value = value;
                }
            }
        }


        public static List<SearchCondition> GetSearchContions(string formData)
        {
            List<SearchCondition> tests = JsonConvert.DeserializeObject<List<SearchCondition>>(formData);
            tests.RemoveAll(t => t.Value == "" || t.Operator == "");
            return tests;
        }

    }

}